package com.xingkeduo.controller.cms.marketing;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.controller.base.MarketingBaseController;
import com.xingkeduo.model.param.MarketingParam;
import com.xingkeduo.model.param.MarketingParam.MarketingCouponStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingReceiveTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingServiceTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingTypeEnum;
import com.xingkeduo.service.cms.marketing.MarketingTemplateService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: ManagementCardController
 * @Description: 市场营销管理-卡项处理相关
 * @author lixin
 * @date Apr 18, 2019 9:46:00 PM
 *
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/card")
public class CardTemplateCmsController extends MarketingBaseController {
	Logger logger = LoggerFactory.getLogger(CardTemplateCmsController.class);
	@Autowired
	@Qualifier(value = "marketingCardTemplateService")
	MarketingTemplateService marketingCardTemplateService;

	@RequestMapping("/addCardTemplate")
	public ResponseMessage addCardTemplate(@RequestBody MarketingParam cardTemplate) {
		cardTemplate.setMarketingType(MarketingTypeEnum.CARD.getCode());
		cardTemplate.setStatus(MarketingStatusEnum.DOWN.getCode());
		try {
			this.initParams(cardTemplate);
			return marketingCardTemplateService.saveOrUpdateCouponByTemplate(cardTemplate);
		} catch (Exception e) {
			logger.error("【添加卡异常】,e:{}",e);
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/getCardTemplateDetail")
	public ResponseMessage getCardTemplateDetail(Long cardTemplateId) {
		if (cardTemplateId == null) {
			return ResponseMessage.error("卡id不合法", 500);
		}
		return marketingCardTemplateService.queryCouponById(cardTemplateId);
	}

	@RequestMapping("/updateCardTemplate")
	public ResponseMessage updateCardTemplate(@RequestBody MarketingParam cardTemplate) {
		cardTemplate.setMarketingType(MarketingTypeEnum.CARD.getCode());
		try {
			this.initParams(cardTemplate);
			return marketingCardTemplateService.saveOrUpdateCouponByTemplate(cardTemplate);
		} catch (Exception e) {
			logger.error("【修改卡异常】,e:{}",e);
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/auditCardTemplate")
	public ResponseMessage auditCardTemplate(Long cardTemplateId, String status) {
		if (cardTemplateId == null) {
			return ResponseMessage.error("卡id不合法", 500);
		}
		if (MarketingStatusEnum.parseStatus(Integer.parseInt(status)).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("卡状态不合法", 500);
		}
		MarketingParam cardTemplate = new MarketingParam();
		cardTemplate.setMarketingType(MarketingTypeEnum.CARD.getCode());
		cardTemplate.setId(cardTemplateId);
		cardTemplate.setStatus(Integer.parseInt(status));
		this.initUserInParams(cardTemplate);
		return marketingCardTemplateService.updateCouponStatusById(cardTemplate);
	}

	@RequestMapping("/getCardTemplateList")
	public ResponseMessage getCardTemplateList(MarketingParam cardTemplateExample) {
		cardTemplateExample.setMarketingType(MarketingTypeEnum.CARD.getCode());
		// 卡类型
		if (StringUtils.isNotBlank(cardTemplateExample.getServiceType()) && MarketingServiceTypeEnum
				.parse(cardTemplateExample.getServiceType()).equals(MarketingServiceTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("卡类型不合法", 500);
		}
		// 卡状态
		if (StringUtils.isNotBlank(cardTemplateExample.getConponStatus()) && MarketingCouponStatusEnum
				.parse(cardTemplateExample.getConponStatus()).equals(MarketingCouponStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("卡状态不合法", 500);
		}
		// 领取范围
		if (cardTemplateExample.getReceiveType() != null && MarketingReceiveTypeEnum
				.parseStatus(cardTemplateExample.getReceiveType()).equals(MarketingReceiveTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("卡领取范围不合法", 500);
		}
		// 上下架
		if (cardTemplateExample.getStatus() != null && MarketingStatusEnum.parseStatus(cardTemplateExample.getStatus())
				.equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("卡上下架状态不合法", 500);
		}
		return marketingCardTemplateService.queryCouponByTemplate(cardTemplateExample);
	}

	@RequestMapping("/getAllCardTemplateList")
	public ResponseMessage getAllCardTemplateList(MarketingParam couponTemplateExample) {
		couponTemplateExample.setMarketingType(MarketingTypeEnum.CARD.getCode());
		return marketingCardTemplateService.getAllCouponTemplateList(couponTemplateExample);
	}
}
